package com.example.qihuibao.widget.dialog

import android.content.DialogInterface
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qihuibao.R
import com.example.qihuibao.base.BaseDialogFragment
import com.example.qihuibao.bean.MealSet
import com.example.qihuibao.bean.RspQueryMealInfoBean
import com.example.qihuibao.widget.view.CommonRecyclerView


/**
 *@author: cww
 *@desc:
 *@date: 5/16/23 10:58 AM
 */
class MealSetSelectDialog(private val list:List<MealSet>, private val dismissBack:(pos:Int)->Unit) : BaseDialogFragment(R.layout.dialog_list_select) {
    private var index = -1

    override fun initView() {
        mView?.findViewById<TextView>(R.id.tv_title)?.text = "选择套餐"
        val rvCanteen = mView?.findViewById<CommonRecyclerView>(R.id.rv_canteen)
        rvCanteen?.init(
            R.layout.item_mealset,
            LinearLayoutManager(context, RecyclerView.VERTICAL, false),
            list,
            {parent, pos ->
                parent.findViewById<TextView>(R.id.tv_mealset_name).text = getString(R.string.mealset_name, list[pos].MealSetName)
                parent.findViewById<TextView>(R.id.tv_mealset_status).text = getString(R.string.mealset_status, if (list[pos].Status=="0") "不开通" else "开通")
                parent.findViewById<TextView>(R.id.tv_mealset_PerOrderLimit).text = getString(R.string.mealset_PerOrderLimit, list[pos].PerOrderLimit)
                parent.findViewById<TextView>(R.id.tv_mealset_PerSpotLimit).text = getString(R.string.mealset_PerSpotLimit, list[pos].PerSpotLimit)
                parent.findViewById<TextView>(R.id.tv_mealset_PerTotalLimit).text = getString(R.string.mealset_PerTotalLimit, list[pos].PerTotalLimit)
                parent.findViewById<TextView>(R.id.tv_mealset_AllOrderLimit).text = getString(R.string.mealset_AllOrderLimit, list[pos].AllOrderLimit)
                parent.findViewById<TextView>(R.id.tv_mealset_AllSpotLimit).text = getString(R.string.mealset_AllSpotLimit, list[pos].AllSpotLimit)
                parent.findViewById<TextView>(R.id.tv_mealset_AllTotalLimit).text = getString(R.string.mealset_AllTotalLimit, list[pos].AllTotalLimit)
                parent.findViewById<ImageView>(R.id.iv_select).visibility = if (index == pos) View.VISIBLE else View.GONE
            },
            {
                index = it
                rvCanteen?.adapter?.notifyDataSetChanged()
            }
        )
        mView?.findViewById<View>(R.id.btn_ok)?.setOnClickListener { dismiss() }
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        if (index == -1) {
            index = 0
        }
        dismissBack.invoke(index)
    }

}